import 'package:douhuaxiangsu/page/palette_list_setting.dart';
import 'package:flutter/material.dart';

import '../entity/artwork_model.dart';
import '../services/global_config_service.dart';
import '../widgets/point_style_dialog.dart';
import '../widgets/toast.dart';
import 'about.dart';

/// 设置页组件
class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  State<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  CanvasConfig canvasConfig = CanvasConfig.fromJson({});

  @override
  void initState() {
    _loadCanvasConfig();
    super.initState();
  }

  _loadCanvasConfig() {
    GlobalConfigService.getCanvasConfig().then(
      (v) => setState(() {
        canvasConfig = v;
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    var ctx = context;
    return Scaffold(
      appBar: AppBar(
        // 设置页标题
        title: const Text('设置'),
        leading: IconButton(
          // 返回按钮
          icon: const Icon(Icons.arrow_back),
          // 返回到上一页
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      body: ListView(
        children: [
          ListTile(
            title: const Text('配置调色板'),
            subtitle: Text('----'),
            onTap: () {
              // 点击后跳转到设置页
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const PaletteSettingListPage(),
                ),
              );
            },
          ),
          ListTile(
            title: const Text('默认坐标设置'),
            subtitle: Text(
              '当前默认状态: ${canvasConfig.showPosition ? '显示' : '不显示'}',
            ),
            onTap: () {
              setState(() {
                canvasConfig.showPosition = !canvasConfig.showPosition;
              });
              GlobalConfigService.setCanvasConfig(canvasConfig);
            },
          ),
          ListTile(
            title: const Text('默认网格设置'),
            subtitle: Text('当前默认状态: ${canvasConfig.showGrid ? '显示' : '不显示'}'),
            onTap: () {
              setState(() {
                canvasConfig.showGrid = !canvasConfig.showGrid;
              });
              GlobalConfigService.setCanvasConfig(canvasConfig);
            },
          ),
          ListTile(
            title: const Text('默认像素点样式'),
            trailing: canvasConfig.pointStyle.getStyleWidget(),
            subtitle: Text('当前: ${canvasConfig.pointStyle.getStyleText()}'),
            onTap: () {
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return PointStyleDialog(
                    canvasConfig: canvasConfig,
                    onStyleChanged: () {
                      setState(() {});
                      GlobalConfigService.setCanvasConfig(canvasConfig);
                    },
                    ctx: ctx,
                  );
                },
              );
            },
          ),
          ListTile(
            title: const Text('设置主题'),
            subtitle: const Text('当前: 默认'),
            onTap: () {
              Toast.show('待开发', context);
            },
          ),
          ListTile(
            title: const Text('设置布局'),
            subtitle: const Text('当前: 默认'),
            onTap: () {
              Toast.show('待开发', context);
            },
          ),
          ListTile(
            title: const Text('关于'),
            subtitle: const Text('豆画像素'),
            onTap: () {
              // 点击后跳转到设置页
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => const AboutPage()),
              );
            },
          ),
        ],
      ),
    );
  }
}
